#include <stdio.h>
#include <stdint.h>
#include <string.h>

// char类型的序列化函数
void serialize_char(char value, uint8_t* buffer) {
    *buffer = (uint8_t)value; // 直接将char值存入缓冲区
}

// char类型的反序列化函数
char deserialize_char(const uint8_t* buffer) {
    return (char)*buffer; // 直接从缓冲区读取char值
}

// 字符串的序列化函数
void serialize_string(const char* str, uint8_t* buffer, size_t buffer_size) {
    size_t len = strlen(str);
    if (len >= buffer_size) {
        len = buffer_size - 1; // 确保不溢出缓冲区
    }
    
    buffer[0] = (uint8_t)len; // 第一个字节存储字符串长度
    memcpy(buffer + 1, str, len); // 后续字节存储字符串内容
}

// 字符串的反序列化函数
void deserialize_string(const uint8_t* buffer, char* str, size_t str_size) {
    size_t len = buffer[0]; // 第一个字节是字符串长度
    if (len >= str_size) {
        len = str_size - 1; // 确保不溢出目标字符串
    }
    
    memcpy(str, buffer + 1, len); // 复制字符串内容
    str[len] = '\0'; // 添加字符串终止符
}

int main() {
    printf("char类型大小: %zu 字节\n\n", sizeof(char));
    
    // 测试单个char的序列化和反序列化
    char test_chars[] = {'A', 'z', '0', 65, 97, -1, 127, -128};
    size_t num_chars = sizeof(test_chars) / sizeof(test_chars[0]);
    
    printf("单个char测试:\n");
    for (size_t i = 0; i < num_chars; i++) {
        char original = test_chars[i];
        uint8_t buffer;
        
        serialize_char(original, &buffer);
        char result = deserialize_char(&buffer);
        
        printf("原始: %c (%d, 0x%02x) -> 序列化: 0x%02x -> 反序列化: %c (%d)\n", 
               original, original, (unsigned char)original, 
               buffer, result, result);
    }
    
    printf("\n字符串测试:\n");
    // 测试字符串的序列化和反序列化
    const char* test_strings[] = {
        "Hello",
        "World",
        "C Programming",
        "A", // 单字符字符串
        ""   // 空字符串
    };
    
    size_t num_strings = sizeof(test_strings) / sizeof(test_strings[0]);
    
    for (size_t i = 0; i < num_strings; i++) {
        const char* original = test_strings[i];
        size_t len = strlen(original);
        
        // 缓冲区大小：1字节长度 + 字符串内容
        uint8_t buffer[256];
        char result[256];
        
        serialize_string(original, buffer, sizeof(buffer));
        deserialize_string(buffer, result, sizeof(result));
        
        printf("原始: \"%s\" (长度: %zu) -> 反序列化: \"%s\" (长度: %zu)\n", 
               original, len, result, strlen(result));
        
        // 显示序列化后的字节内容
        printf("序列化字节: [长度: 0x%02x] ", buffer[0]);
        for (size_t j = 1; j <= len && j < 10; j++) {
            printf("0x%02x ", buffer[j]);
        }
        if (len > 9) printf("...");
        printf("\n\n");
    }
    
    // 测试带特殊字符的字符串
    printf("特殊字符测试:\n");
    char special_chars[] = {0x00, 0x7F, 0xFF, 0x0A, 0x0D}; // NULL, DEL, ÿ, LF, CR
    char special_str[6];
    memcpy(special_str, special_chars, 5);
    special_str[5] = '\0';
    
    uint8_t special_buffer[256];
    char special_result[256];
    
    serialize_string(special_str, special_buffer, sizeof(special_buffer));
    deserialize_string(special_buffer, special_result, sizeof(special_result));
    
    printf("原始特殊字符串: ");
    for (int i = 0; i < 5; i++) {
        printf("0x%02x ", (unsigned char)special_str[i]);
    }
    printf("\n");
    
    printf("反序列化特殊字符串: ");
    for (int i = 0; i < 5; i++) {
        printf("0x%02x ", (unsigned char)special_result[i]);
    }
    printf("\n");
    
    return 0;
}